package com.bawei.yangzongcai___day2.MaiDongFangHong.day7;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bawei.yangzongcai___day2.R;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import java.util.ArrayList;
import java.util.List;

@Route(path = "/shujuku/laile")
public class MainDongFangHong702 extends AppCompatActivity implements OnItemChildClickListener {
    private RecyclerView meile;
    List<Jcalcals2> listclass;
    Shiperiqiadpter qaqfour;
    SQLiteDatabase read;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_dong_fang_hong702);
        initView();
        meile.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));

        SQLdongfanhongqi2 sqLyongyong= new SQLdongfanhongqi2(MainDongFangHong702.this,"yang.dp",null,2);
         read= sqLyongyong.getReadableDatabase();
        Cursor chenchen = read.query("yang", null, null, null, null, null, null);
        listclass = new ArrayList<>();
        while (chenchen.moveToNext())
        {
            String nana = chenchen.getString(chenchen.getColumnIndex("nana"));
            String tutu = chenchen.getString(chenchen.getColumnIndex("tutu"));
            Jcalcals2 jinxi = new Jcalcals2(nana,tutu);
            listclass.add(jinxi);

        }
        qaqfour = new Shiperiqiadpter(listclass);
        qaqfour.setOnItemChildClickListener(this);
        meile.setAdapter(qaqfour);


    }

    private void initView() {

        meile = findViewById(R.id.meile);
    }



    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, final int position) {
        switch (view.getId())
        {
            case R.id.dongfang7img:
               AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setIcon(R.mipmap.ic_launcher);
                builder.setMessage("是否确定删除该数据？");
                builder.setTitle("执行删除");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ToastUtils.showShort("数据已删除");
                        read.execSQL("delete from yang where tutu=?",new Object[]{listclass.get(position).getTutu()});
                        listclass.remove(position);
                        qaqfour.notifyDataSetChanged();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ToastUtils.showShort("取消了");
                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
                break;
        }
    }
}
